package com.qianguo.shop.fragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.widget.CircleImageView;
import com.qianguo.shop.R;
import com.qianguo.shop.base.BaseFragment;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;
import com.eva.androidlibrary.api.entity.OrderInfo;
import com.qianguo.shop.entity.OrderList;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * @author GUXIA
 * @since 2015-10-11
 */
@EFragment(R.layout.fragment_done_order)
public class DoneOrderFragment extends BaseFragment {

    @ViewById(R.id.swipe_refresh_recycler_view)
    SwipeRefreshRecyclerView mSwipeRefreshRecyclerView;

    private DoneOrderAdapter mAdapter = null;
    private List<OrderInfo> mOrders = new ArrayList<>();
    private int mCurrentPage = 1;
    private int mTotal;
    private static final int PER_PAGE = 10;

    @Override
    protected void initializeUI() {
        final Context ctx = getApplicationContext();
        mAdapter = new DoneOrderAdapter(ctx);
        mSwipeRefreshRecyclerView.setAdapter(mAdapter);
        mSwipeRefreshRecyclerView.setSwipeRefreshListener(new SwipeRefreshRecyclerView.SwipeRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mOrders.clear();
                loadData(mCurrentPage, PER_PAGE);
            }

            @Override
            public void onLoadMore() {
                loadData(++mCurrentPage, PER_PAGE);
            }
        });

        loadData(mCurrentPage, PER_PAGE);
    }

    @Background
    void loadData(int page, int perPage) {
        mApp.getApi().getOrders(mApp.getUser().getToken(), "", OrderInfo.STATUS_DONE, page, perPage, new HttpRequestCallback<OrderList>() {
            @Override
            public void onSuccess(int code, String msg, OrderList data) {
                mTotal = data.getTotal();
                mOrders.addAll(data.getList());
                refreshUI();
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    @UiThread
    void refreshUI() {
        mSwipeRefreshRecyclerView.onCompleted();
        mSwipeRefreshRecyclerView.notifyDataSetChanged();
    }


    private class DoneOrderAdapter extends RecyclerView.Adapter<DoneOrderViewHolder> {

        private Context mContext;
        private DoneOrderAdapter(Context context) {
            mContext = context;
        }

        @Override
        public DoneOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_done_order, parent, false);
            return new DoneOrderViewHolder(v);
        }

        @Override
        public void onBindViewHolder(DoneOrderViewHolder holder, int position) {
            holder.bind(mOrders.get(position));
        }

        @Override
        public int getItemCount() {
            return mOrders != null ? mOrders.size() : 0;
        }
    }

    private class DoneOrderViewHolder extends RecyclerView.ViewHolder {

        CircleImageView avatar;
        TextView tvUserName;
        TextView tvAddress;
        TextView tvDeliveryTime;
        TextView tvOrderTime;
        TextView tvFinishTime;
        TextView tvPrice;

        public DoneOrderViewHolder(View itemView) {
            super(itemView);
            avatar = (CircleImageView) itemView.findViewById(R.id.done_order_avatar);
            tvUserName = (TextView) itemView.findViewById(R.id.done_order_username);
            tvAddress = (TextView) itemView.findViewById(R.id.done_order_addr_value);
            tvDeliveryTime = (TextView) itemView.findViewById(R.id.done_order_date_handle_value);
            tvOrderTime = (TextView) itemView.findViewById(R.id.done_order_date_added_value);
            tvFinishTime = (TextView) itemView.findViewById(R.id.done_order_date_done_value);
            tvPrice = (TextView) itemView.findViewById(R.id.done_order_amount);
        }

        public void bind(OrderInfo item) {
            mImageLoader.loadImage(getActivity(), avatar, item.getAvatar());
            tvUserName.setText(item.getUserName());
            tvAddress.setText(item.getReceiverAddress());
            tvDeliveryTime.setText(item.getDeliveryTime());
//            tvOrderTime.setText(item.getOrderTime());
            tvFinishTime.setText(item.getOrderFinishTime());
            tvPrice.setText(String.valueOf(item.getPrice()));
        }
    }
}
